// run: time $exec < input
#include <iostream>
#include <vector>
#include <stack>

struct node
{
	int index, lowlink, color;
	bool on_stack, visited;
};

long long ans;
long long mo = 1000000007;

int n, index, color/*strong connected compoent index*/;
int k;

using edge_type = std::vector<int>;
std::vector<edge_type> graph;
std::vector<node> nodes;
std::stack<int> s;
std::vector<int> out_dgree;
std::vector<int> in_dgree;

void tarjan(int v)
{
	nodes[v].index = nodes[v].lowlink = ++index;
	nodes[v].visited = nodes[v].on_stack = true;
	s.push(v);

	for (auto it = graph[v].begin(); it != graph[v].end(); ++it) {
		int w = *it;
		if (!nodes[w].visited) {
			tarjan(w);
			nodes[v].lowlink = std::min(nodes[v].lowlink, nodes[w].lowlink);
		} else
		if (nodes[w].on_stack)
			nodes[v].lowlink = std::min(nodes[v].lowlink, nodes[w].index);
	}

	if (nodes[v].lowlink == nodes[v].index) {
		int w; color++;
		do {
			w = s.top(); s.pop();
			nodes[w].on_stack = false;
			nodes[w].color = color;
		} while (w != v);
	}
}


struct node_2
{
	int parent, rank, ancestor, color;
};


typedef int index_2;
typedef std::pair<int, index_2> node_type;
std::vector<std::vector<int> > cg;
std::vector<std::vector<int> > common_pair;

int const maxn = 100007;
node_2 every[maxn];
bool vis[maxn];

int tree_tag[maxn];

void make_set(int u)
{
	every[u].parent = u;
	every[u].rank = 0;
}

int find(int u)
{
	return (every[u].parent == u) ? u : (every[u].parent = find(every[u].parent));
}

void set_union(int u, int v)
{
	int uroot = find(u);
	int vroot = find(v);
	if (every[uroot].rank > every[vroot].rank)
		every[uroot].parent = vroot;
	else if (uroot != vroot) {
		every[vroot].parent = uroot;
		every[uroot].rank++;
	}
}

void tarjan_offline_lca(int u)
{
	vis[u] = true;
	make_set(u);
	every[u].ancestor = u;
	for (int i = 0; i < (int)cg[u].size(); i++) {
		int v = cg[u][i];
		if (vis[v]) continue;
		tarjan_offline_lca(v);
		set_union(u, v);
		every[find(u)].ancestor = u;
	}
	every[u].color = 1;

	for (int i = 0; i < (int)common_pair[u].size(); i++) {
		int v = common_pair[u][i];
		if (every[v].color) { // FIXME
			int y = every[find(v)].ancestor;
			tree_tag[y]++;
			tree_tag[y]--;
		}
	}
}

long long quick(long long x)
{
	if (!x) return 1;
	long long tmp = quick(x / 2);
	tmp *= tmp;
	tmp %= mo;
	if (x & 1) tmp = (tmp * x) % mo;
	return tmp;
}

long long calc_tag(int u)
{
	long long ret = tree_tag[u];
	for (auto i : cg[u]) {
		int v = i;
		long long s = calc_tag(v);
		ans += ((quick(s) + mo) % mo);
		ans %= mo;
		ret += s;
	}
	return ret;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	graph.resize(n);
	nodes.resize(n);
	for (int i = 1, x, y, c; i < n; i++) {
		std::cin >> x >> y >> c;
		x--; y--;
		graph[x].push_back(y);
		if (!c) graph[y].push_back(x);
	}

	for (int i = 0; i < n; i++)
		if (!nodes[i].visited) tarjan(i);

	cg.resize(color + 4);
	common_pair.resize(color + 4);
	for (int i = 0; i < n; i++)
		for (auto j : graph[i]) {
			if (nodes[i].color != nodes[j].color) {
				int x = nodes[i].color, y = nodes[j].color;
				cg[x].push_back(y);
				std::cerr << x << " " << y << '\n';
			}
		}

	std::cin >> k;
	int prev = 0;
	for (int i = 0, x; i < k; i++) {
		std::cin >> x;
		x--;
		common_pair[nodes[prev].color].push_back(nodes[x].color);
		common_pair[nodes[x].color].push_back(nodes[prev].color);
		prev = x;
	}

	tarjan_offline_lca(0);

	calc_tag(0);
	std::cout << ans << '\n';
}

